// *****************************************************
// Aitrox Information Technology
// http://www.proxima-ai.com
// Copyright 2021 Aitrox. All rights reserved.
//
// Filename: export.go
// CreateDate: 2023-05-06
// Author: zjp
// Description: 定义返回数据类型
// ******************************************************
package export

import (
	"fmt"
	"usplatformserver/internal/model"

	"github.com/jinzhu/gorm"
)

type fileNameInfo struct {
	FileName string `json:"fileName"`
}

type ExportSwaggerSuccess struct {
	*model.SwaggerSuccess
	Data *fileNameInfo `json:"data"`
}

type ClientUsageStatistics struct {
	CheckId       string `json:"check_id" gorm:"Column:check_id"`
	CheckDateTime string `json:"operateTime" gorm:"Column:operateTime"`
	EventId       int    `json:"eventId" gorm:"Column:eventId"`
	PatientStamp  string `json:"timeStamp" gorm:"Column:timeStamp"`
	PatientId     int    `json:"patientId" gorm:"Column:patientId"`
}

/**
 * @brief 获取使用率数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetUsageExportData(db *gorm.DB, patientIds string) ([]*ClientUsageStatistics, error) {
	var out []*ClientUsageStatistics

	var query string
	query = "SELECT t1.id as patientId, t1.timeStamp, t1.check_id, t2.eventId, t2.operateTime "
	query += " FROM t_patient_info t1"
	query += " INNER JOIN t_log_info t2 on t1.id=t2.patientId"
	query += fmt.Sprintf(" WHERE t1.id in (%s)", patientIds)
	query += " ORDER BY t1.id asc"

	res := db.Raw(query).Scan(&out)
	if res.Error != nil {
		return nil, res.Error
	}

	return out, nil
}
